package com.csdj.controller.Be;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.csdj.common.enums.AppHttpCodeEnum;
import com.csdj.common.utils.ResponseData;
import com.csdj.common.utils.ResponseResult;
import com.csdj.common.utils.tool;
import com.csdj.dto.OrderTypes;
import com.csdj.dto.orderPay;
import com.csdj.pojo.BeHistory;
import com.csdj.pojo.BeOrder;
import com.csdj.pojo.BeOrderuser;
import com.csdj.service.BeHistoryService;
import com.csdj.service.BeOrderService;
import com.csdj.service.BeOrderuserService;
import com.csdj.service.SyMetertypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.*;

/**
 * BE_Order业扩工程单表
 * @author 段文海
 */
@RestController
@RequestMapping("beOrder")
public class BeOrderController {
    //实例
    @Autowired
    private BeOrderService beOrderService;
    @Autowired
    private BeOrderuserService beOrderuserService;
    @Autowired
    private SyMetertypeService syMetertypeService;
    @Autowired
    private BeHistoryService beHistoryService;
    /**
     * 办理新户->工程单
     */
    @PostMapping("/new")
    public ResponseResult insertNew(@RequestBody BeOrder beOrder){
        beOrder.setStepID(1);
        beOrder.setCreateDate(new Date());
        beOrder.setLastEditDate(new Date());
        beOrder.setAreaID(2);
        if(beOrder.getOrderType()==1 || beOrder.getOrderType()==2 || beOrder.getOrderType()==3){
            beOrder.setProjectMoney( BigDecimal.valueOf(beOrder.getMeterCount()).multiply(BigDecimal.valueOf(3000)));
        }
        if(beOrder.getOrderType()==1) {
            beOrder.setOrderNo(tool.getOrderNo("B1"));
            if (!beOrderService.save(beOrder)) {
                return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR, "1服务器正在忙碌中,请稍后重试");
            }
        }else{
            if(!beOrderService.updateById(beOrder)){
                return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR, "1服务器正在忙碌中,请稍后重试");
            }
        }
        //流程记录
        BeHistory history=new BeHistory();
        history.setOrderNo(beOrder.getOrderNo());
        history.setStepID(beOrder.getStepID());
        history.setDatee(new Date());
        history.setStepID(1);
        history.setEmpID(beOrder.getLastEditEmp());
        beHistoryService.save(history);
       return ResponseResult.okResult(beOrder.getOrderNo());
    }



    /**
     * 新户信息
     */
    @PostMapping("/new1")
    public ResponseResult new1(@RequestBody Map<String, Object> map){
        ArrayList beOrderUserList = JSON.parseObject(JSON.toJSONString(map.get("BeOrderUserList")), ArrayList.class);
        ArrayList<BeOrderuser> orderusers = new ArrayList<>(beOrderUserList.size());
        String hhh=map.get("hhh").toString();//获取业扩工单no
        BeOrder beOrder = beOrderService.getById(hhh);
        for (Object o : beOrderUserList) {
            BeOrderuser beOrderuser = JSON.parseObject( o.toString(), BeOrderuser.class);
            beOrderuser.setOrderNo(hhh);
            beOrderuser.setMeterTypeID(beOrder.getMeterTypeID());
            beOrderuser.setProjectMoney(3000.0);
            beOrderuser.setUserNo(tool.getUserNo());
            orderusers.add(beOrderuser);
        }

        if (!beOrderuserService.saveBatch(orderusers)) {
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR,"服务器正在忙碌中,请稍后重试");
        }
        return ResponseResult.okResult("添加成功!");
    }
    /**
     * 初步审核 分页
     */
    @GetMapping("/page")
    public ResponseResult pages(@RequestParam(defaultValue = "1") int pageNum ,
                                @RequestParam(defaultValue = "10") int pageSize){
        Page<BeOrder> page= new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<BeOrder> l=new LambdaQueryWrapper<>();
        l.orderByDesc(BeOrder::getLastEditDate);
        l.eq(BeOrder::getStepID,1);
        IPage<BeOrder> pages=beOrderService.page(page,l);
        return ResponseResult.okResult(pages);
    }

    /**
     * 根据工单号获取工单
     * @param orderNo 工单号
     */
    @GetMapping("/getById")
    public ResponseResult getById(String orderNo){
        BeOrder byId = beOrderService.getByIdCx(orderNo);
        return byId.getOrderNo()!=null?ResponseResult.okResult(byId):ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR,"服务器正在忙碌中国，稍后重试");
    }
    /**
     * 初步审核->发送
     */
    @PostMapping("/send")
    public ResponseResult send(@RequestBody BeOrder beOrder){
        BeHistory history = new BeHistory();
        history.setOrderNo(beOrder.getOrderNo());
        history.setStepID(2);
        history.setDatee(new Date());
        history.setEmpID(beOrder.getLastEditEmp());
        beOrder.setStepID(2);
        beOrder.setLastEditDate(new Date());
        beHistoryService.save(history);
        return beOrderService.updateById(beOrder)?ResponseResult.okResult("发送成功!"):ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR,"发送失败!");
    }

    /**
     * 初步审核->撤回
     */
    @PostMapping("/revocation")
    public ResponseResult revocation(@RequestBody BeOrder beOrder){
        beHistoryService.getRemovePojo(beOrder.getOrderNo());
        return beOrderService.removeById(beOrder)?ResponseResult.okResult("撤回成功!"):ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR,"撤回失败!");
    }

    /**
     * 获取该工程单所有流程记录
     */
    @GetMapping("/history")
    public ResponseResult history( String oid){
        List<BeHistory> list = beHistoryService.getAll(oid);
        return list.isEmpty()?ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR,"服务器繁忙!"):ResponseResult.okResult(list);
    }
    /**
     * 交施工费的数据清单
     */
    @GetMapping("/sgf")
    public ResponseResult pageSgf(@RequestParam(defaultValue = "1") int pageNum ,
                                @RequestParam(defaultValue = "10") int pageSize){
        Page<BeOrder> page= new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<BeOrder> l=new LambdaQueryWrapper<>();
        l.orderByDesc(BeOrder::getLastEditDate);
        l.eq(BeOrder::getStepID,2);
        l.in(BeOrder::getOrderType,1,2,5);
        IPage<BeOrder> pages=beOrderService.page(page,l);
        return ResponseResult.okResult(pages);
    }
    /**
     * 交施工费->发送
     */
    @PostMapping("/jsgfsend")
    public ResponseResult jsgf(@RequestBody Map<Object,Object> map){
        ArrayList beOrderUserList = JSON.parseObject(JSON.toJSONString(map.get("orderByUser")), ArrayList.class);
        String status = map.get("status").toString();
        ArrayList<BeOrderuser> orderusers = new ArrayList<>(beOrderUserList.size());
        Double mouny= 0.0;
        for (int i = 0; i < beOrderUserList.size(); i++) {
            BeOrderuser beOrderuser = JSON.parseObject(JSON.toJSONString(beOrderUserList.get(i)), BeOrderuser.class);
            if(beOrderuser.getRealMoney()!=null) {
                mouny += beOrderuser.getRealMoney();
            }else{
                mouny+=0;
            }
            orderusers.add(beOrderuser);
        }
        BeOrder beOrder = JSON.parseObject(JSON.toJSONString(map.get("order")), BeOrder.class);
        if(!status.equals("0")){
            if(beOrder.getProjectMoney().intValue()==beOrder.getRealMoney().intValue()){
                BeHistory history = new BeHistory();
                history.setOrderNo(beOrder.getOrderNo());
                history.setStepID(3);
                history.setDatee(new Date());
                beOrder.setLastEditDate(new Date());
                history.setEmpID(beOrder.getLastEditEmp());
                beHistoryService.save(history);
                beOrder.setStepID(3);
            }
        }
        beOrder.setRealMoney(BigDecimal.valueOf(mouny));

        beOrderService.updateById(beOrder);

        return beOrderuserService.updateBatchById(orderusers)?ResponseResult.okResult("发送成功!"):ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR,"发送失败!");
    }

    /**
     * 交施工费->撤回
     */
    @PostMapping("/jsgfrevocation")
    public ResponseResult jsgfc(@RequestBody BeOrder beOrder){
        beOrder.setAuditEmpID(null);
        beOrder.setAuditMessage("");
        beOrder.setStepID(1);
        beOrder.setRealMoney(BigDecimal.valueOf(0));
        beHistoryService.getRemovePojo(beOrder.getOrderNo());
        LambdaQueryWrapper<BeOrderuser> l=new LambdaQueryWrapper<>();
        l.eq(BeOrderuser::getOrderNo,beOrder.getOrderNo());
        List<BeOrderuser> list = beOrderuserService.list(l);
        for (int i = 0; i < list.size(); i++) {
            list.get(i).setRealMoney(0.0);
        }
        beOrderuserService.updateBatchById(list);
        return beOrderService.updateById(beOrder)?ResponseResult.okResult("撤回成功!"):ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR,"撤回失败!");
    }
    /**
     * 水费清算的数据
     */
    @GetMapping("sfqs")
    public ResponseResult pagesfqs(@RequestParam(defaultValue = "1") int pageNum ,
                                  @RequestParam(defaultValue = "10") int pageSize){
        Page<BeOrder> page= new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<BeOrder> l=new LambdaQueryWrapper<>();
        l.orderByDesc(BeOrder::getLastEditDate);
        l.in(BeOrder::getStepID,3,2);
        l.in(BeOrder::getOrderType,2,7);
        IPage<BeOrder> pages=beOrderService.page(page,l);
        List<BeOrder> records = pages.getRecords();
        for (int i = records.size() - 1; i >= 0; i--) {
            if (records.get(i).getStepID() == 2 && records.get(i).getOrderType() != 7) {
                records.remove(i);
            }
        }
        return ResponseResult.okResult(pages);
    }
    /**
     * 水费清算->发送
     */
    @PostMapping("/sfqssend")
    public ResponseResult sfqs(@RequestBody BeOrder beOrder){
        BeHistory history = new BeHistory();
        history.setOrderNo(beOrder.getOrderNo());
        beOrder.setLastEditDate(new Date());
        history.setStepID(4);
        history.setDatee(new Date());
        history.setEmpID(beOrder.getLastEditEmp());
        beHistoryService.save(history);
        beOrder.setStepID(4);
        return beOrderService.updateById(beOrder)?ResponseResult.okResult("发送成功!"):ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR,"发送失败!");
    }

    /**
     * 水费清算->撤回
     */
    @PostMapping("/sfqsrevocation")
    public ResponseResult sfqsc(@RequestBody BeOrder beOrder){
        beOrder.setAuditEmpID(null);
        beOrder.setAuditMessage("");
        if(beOrder.getOrderType()==2) {
            beOrder.setStepID(2);
        }else{
            beOrder.setStepID(1);
        }
        beHistoryService.getRemovePojo(beOrder.getOrderNo());
        return beOrderService.updateById(beOrder)?ResponseResult.okResult("撤回成功!"):ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR,"撤回失败!");
    }
    /**
     * 供水协议的数据
     */
    @GetMapping("gsxy")
    public ResponseResult pagegsxy(@RequestParam(defaultValue = "1") int pageNum ,
                                   @RequestParam(defaultValue = "10") int pageSize){
        Page<BeOrder> page= new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<BeOrder> l=new LambdaQueryWrapper<>();
        l.orderByDesc(BeOrder::getLastEditDate);
        l.in(BeOrder::getStepID,4,3,2);
        l.in(BeOrder::getOrderType,1,2,6);
        IPage<BeOrder> pages=beOrderService.page(page,l);
        List<BeOrder> records = pages.getRecords();
        for (int i = records.size() - 1; i >= 0; i--) {
            if (records.get(i).getStepID() == 3 && records.get(i).getOrderType() != 1) {
                records.remove(i);
                continue;
            }
            if (records.get(i).getStepID() == 2 && records.get(i).getOrderType() != 6) {
                records.remove(i);
                continue;
            }
        }
        return ResponseResult.okResult(pages);
    }
    /**
     * 供水协议->发送
     */
    @PostMapping("/gsxysend")
    public ResponseResult gsxy(@RequestBody Map<Object,Object> map){
        ArrayList beOrderUserList = JSON.parseObject(JSON.toJSONString(map.get("orderByUser")), ArrayList.class);
        BeOrder beOrder = JSON.parseObject(JSON.toJSONString(map.get("order")), BeOrder.class);
        String status = map.get("status").toString();
        ArrayList<BeOrderuser> orderusers = new ArrayList<>(beOrderUserList.size());
        for (int i = 0; i < beOrderUserList.size(); i++) {
            BeOrderuser beOrderuser = JSON.parseObject(JSON.toJSONString(beOrderUserList.get(i)), BeOrderuser.class);
            orderusers.add(beOrderuser);
        }
        if(!status.equals("0")){
            beOrder.setStepID(5);
            beOrder.setLastEditDate(new Date());
            BeHistory history = new BeHistory();
            history.setOrderNo(beOrder.getOrderNo());
            history.setStepID(5);
            history.setDatee(new Date());
            history.setEmpID(beOrder.getLastEditEmp());
            beHistoryService.save(history);
        }
        beOrderService.updateById(beOrder);
        return beOrderuserService.updateBatchById(orderusers)?ResponseResult.okResult("发送成功!"):ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR,"发送失败!");
    }

    /**
     * 供水协议->撤回
     */
    @PostMapping("/gsxyrevocation")
    public ResponseResult gsxyrevocation(@RequestBody BeOrder beOrder){
        beOrder.setAuditEmpID(null);
        beOrder.setAuditMessage("");
        if(beOrder.getOrderType()==1) {
            beOrder.setStepID(2);
        }else if(beOrder.getOrderType()==6){
            beOrder.setStepID(1);
        }else {
            beOrder.setStepID(3);
        }
        LambdaQueryWrapper<BeOrderuser> l=new LambdaQueryWrapper<>();
        l.eq(BeOrderuser::getOrderNo,beOrder.getOrderNo());
        List<BeOrderuser> list = beOrderuserService.list(l);
        for (int i = 0; i < list.size(); i++) {
            list.get(i).setFormula(null);
            list.get(i).setContractNum(null);
        }
        beOrderuserService.updateBatchById(list);
        beHistoryService.getRemovePojo(beOrder.getOrderNo());
        return beOrderService.updateById(beOrder)?ResponseResult.okResult("撤回成功!"):ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR,"撤回失败!");
    }


    /**
     * 施工竣工的数据
     */
    @GetMapping("sgjg")
    public ResponseResult pagesgjg(@RequestParam(defaultValue = "1") int pageNum ,
                                   @RequestParam(defaultValue = "10") int pageSize){
        Page<BeOrder> page= new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<BeOrder> l=new LambdaQueryWrapper<>();
        l.orderByDesc(BeOrder::getLastEditDate);
        l.in(BeOrder::getStepID,5,3);
        l.in(BeOrder::getOrderType,1,2,5);
        IPage<BeOrder> pages=beOrderService.page(page,l);
        List<BeOrder> records = pages.getRecords();
        for (int i = records.size() - 1; i >= 0; i--) {
            if (records.get(i).getStepID() == 3 && records.get(i).getOrderType() != 5) {
                records.remove(i);
            }
        }
        return ResponseResult.okResult(pages);
    }
    /**
     * 施工竣工->发送?保存不发送
     */
    @PostMapping("/sgjgsend")
    public ResponseResult sgjg(@RequestBody Map<Object,Object> map){
        ArrayList beOrderUserList = JSON.parseObject(JSON.toJSONString(map.get("orderByUser")), ArrayList.class);
        String status = map.get("status").toString();
        BeOrder beOrder = JSON.parseObject(JSON.toJSONString(map.get("order")), BeOrder.class);
        if(!status.equals("0")){
            beOrder.setStepID(6);
            BeHistory history = new BeHistory();
            history.setOrderNo(beOrder.getOrderNo());
            history.setStepID(6);
            history.setDatee(new Date());
            history.setEmpID(beOrder.getLastEditEmp());
            beHistoryService.save(history);
        }
        ArrayList<BeOrderuser> orderusers = new ArrayList<>(beOrderUserList.size());
        for (int i = 0; i < beOrderUserList.size(); i++) {
            BeOrderuser beOrderuser = JSON.parseObject(JSON.toJSONString(beOrderUserList.get(i)), BeOrderuser.class);
            orderusers.add(beOrderuser);
        }
        beOrderService.updateById(beOrder);
        return beOrderuserService.updateBatchById(orderusers)?ResponseResult.okResult("发送成功!"):ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR,"发送失败!");
    }

    /**
     * 施工竣工->撤回
     */
    @PostMapping("/sgjgrevocation")
    public ResponseResult sgjg(@RequestBody BeOrder beOrder){
        beOrder.setAuditEmpID(null);
        beOrder.setAuditMessage("");
        if(beOrder.getOrderType()==5) {
            beOrder.setStepID(1);
        }else if(beOrder.getOrderType()==1){
            beOrder.setStepID(3);
        }else{
            beOrder.setStepID(4);
        }
        beHistoryService.getRemovePojo(beOrder.getOrderNo());
        return beOrderService.updateById(beOrder)?ResponseResult.okResult("撤回成功!"):ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR,"撤回失败!");
    }
    /**
     * 通水停水的数据
     */
    @GetMapping("tsts")
    public ResponseResult pagetsts(@RequestParam(defaultValue = "1") int pageNum ,
                                   @RequestParam(defaultValue = "10") int pageSize){
        Page<BeOrder> page= new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<BeOrder> l=new LambdaQueryWrapper<>();
        l.orderByDesc(BeOrder::getLastEditDate);
        l.in(BeOrder::getStepID,6,4);
        l.in(BeOrder::getOrderType,1,2,5,7);
        IPage<BeOrder> pages=beOrderService.page(page,l);
        List<BeOrder> records = pages.getRecords();
        for (int i = records.size() - 1; i >= 0; i--) {
            if (records.get(i).getStepID() == 4 && records.get(i).getOrderType() != 7) {
                records.remove(i);
            }
        }
        return ResponseResult.okResult(pages);
    }
    /**
     * 通水停水->发送
     */
    @PostMapping("/tsts")
    public ResponseResult tsts(@RequestBody BeOrder beOrder){
        BeHistory history = new BeHistory();
        history.setOrderNo(beOrder.getOrderNo());
        history.setStepID(7);
        history.setDatee(new Date());
        history.setEmpID(beOrder.getLastEditEmp());
        beOrder.setStepID(7);
        beHistoryService.save(history);
        beOrder.setLastEditDate(new Date());
        return beOrderService.updateById(beOrder)?ResponseResult.okResult("发送成功!"):ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR,"发送失败!");
    }
    /**
     * 通水停水->撤回
     */
    @PostMapping("/tstsch")
    public ResponseResult tstsch(@RequestBody BeOrder beOrder){
        beOrder.setAuditEmpID(null);
        beOrder.setAuditMessage("");
        if(beOrder.getOrderType()==7){
            beOrder.setStepID(2);
        }else {
            beOrder.setStepID(5);
        }
        beHistoryService.getRemovePojo(beOrder.getOrderNo());
        return beOrderService.updateById(beOrder)?ResponseResult.okResult("撤回成功!"):ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR,"撤回失败!");
    }
    /**
     * 档案存档的数据
     */
    @GetMapping("dacd")
    public ResponseResult pagedacd(@RequestParam(defaultValue = "0") int pageNum ,
                                   @RequestParam(defaultValue = "10") int pageSize){
        Page<BeOrder> page= new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<BeOrder> l=new LambdaQueryWrapper<>();
        l.orderByDesc(BeOrder::getLastEditDate);
        l.in(BeOrder::getStepID,7,2,5);
        IPage<BeOrder> pages=beOrderService.page(page,l);
        List<BeOrder> records = pages.getRecords();
        for (int i= records.size() - 1;i>=0; i--) {
            if (records.get(i).getStepID() ==2   && (records.get(i).getOrderType() != 3 || records.get(i).getOrderType()!=4)) {
                records.remove(i);
                continue;
            }
            if( records.get(i).getStepID()==5 && records.get(i).getOrderType()!=6){
                records.remove(i);
                continue;
            }
        }
        return ResponseResult.okResult(pages);
    }
    /**
     * 档案存档->发送
     */
    @PostMapping("/tacd")
    public ResponseResult tacd(@RequestBody Map<Object,Object> map){
        ArrayList beOrderUserList = JSON.parseObject(JSON.toJSONString(map.get("orderByUser")), ArrayList.class);
        ArrayList<BeOrderuser> orderusers = new ArrayList<>(beOrderUserList.size());
        BeOrder beOrder = JSON.parseObject(JSON.toJSONString(map.get("order")), BeOrder.class);
        String status = map.get("status").toString();
        for (int i = 0; i < beOrderUserList.size(); i++) {
            BeOrderuser beOrderuser = JSON.parseObject(JSON.toJSONString(beOrderUserList.get(i)), BeOrderuser.class);
            orderusers.add(beOrderuser);
        }
        if(!status.equals("0")){
            beOrder.setStepID(8);
            BeHistory history = new BeHistory();
            history.setOrderNo(beOrder.getOrderNo());
            history.setStepID(8);
            history.setDatee(new Date());
            history.setEmpID(beOrder.getLastEditEmp());
            beHistoryService.save(history);
            beOrder.setLastEditDate(new Date());
        }
        beOrderService.updateById(beOrder);
        return beOrderuserService.updateBatchById(orderusers)?ResponseResult.okResult("发送成功!"):ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR,"发送失败!");
    }
    /**
     * 档案存档->撤回
     */
    @PostMapping("/tacdch")
    public ResponseResult tacdch(@RequestBody Map<Object,Object> map){
        ArrayList beOrderUserList = JSON.parseObject(JSON.toJSONString(map.get("orderByUser")), ArrayList.class);
        ArrayList<BeOrderuser> orderusers = new ArrayList<>(beOrderUserList.size());
        BeOrder beOrder = JSON.parseObject(JSON.toJSONString(map.get("order")), BeOrder.class);
        for (int i = 0; i < beOrderUserList.size(); i++) {
            BeOrderuser beOrderuser = JSON.parseObject(JSON.toJSONString(beOrderUserList.get(i)), BeOrderuser.class);
            orderusers.add(beOrderuser);
            orderusers.get(i).setDocNum(null);
        }
        beOrder.setAuditEmpID(null);
        beOrder.setAuditMessage("");
        if(beOrder.getOrderType()==3 || beOrder.getOrderType()==4){
            beOrder.setStepID(1);
        }else if(beOrder.getOrderType()==6){
            beOrder.setStepID(2);
        }else{
            beOrder.setStepID(6);
        }
        beOrderService.updateById(beOrder);
        beHistoryService.getRemovePojo(beOrder.getOrderNo());
       return beOrderuserService.updateBatchById(orderusers)?ResponseResult.okResult("撤回成功!"):ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR,"撤回失败!");
    }
    /**
     * 工单管理的数据
     */
    @GetMapping("gdgl")
    public ResponseResult pagegdgl(@RequestParam(defaultValue = "1") int pageNum ,
                                   @RequestParam(defaultValue = "10") int pageSize,
                                   @RequestParam(defaultValue = "0")Integer deptID,//部门id
                                   @RequestParam(defaultValue = "")String orderNo,//工单号
                                   @RequestParam(defaultValue = "")String userName,//用户姓名
                                   @RequestParam(defaultValue = "0")Integer orderType,//工单类型
                                   @RequestParam(defaultValue = "10")Integer orderJd//工程进度
    ){
        Page<BeOrder> page= new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<BeOrder> l=new LambdaQueryWrapper<>();
        l.orderByDesc(BeOrder::getLastEditDate);
        if(deptID!=0){
            switch (deptID){
                case 2:
                    l.in(BeOrder::getStepID,1,4,7);
                    break;
                case 3:
                    l.eq(BeOrder::getStepID,2);
                    break;
                case 4:
                    l.eq(BeOrder::getStepID,3);
                    break;
                case 5:
                    l.eq(BeOrder::getStepID,6);
                    break;
                case 7:
                    l.eq(BeOrder::getStepID,5);
                    break;
            }
        }
        if(!orderNo.equals("")){
            l.eq(BeOrder::getOrderNo,orderNo);
        }
        if(!userName.equals("")){
            l.like(BeOrder::getUserName,userName);
        }
        if(orderType!=0){
            l.eq(BeOrder::getOrderType,orderType);
        }
        if(orderJd!=10){
            l.eq(BeOrder::getStepID,orderJd);
        }
        IPage<BeOrder> pages=beOrderService.page(page,l);
        return ResponseResult.okResult(pages);
    }
    /**
     * 终止工单的数据
     */
    @GetMapping("/zzgd")
    public ResponseResult zzgdByNo(String orderNo){
        LambdaQueryWrapper<BeOrder> l=new LambdaQueryWrapper<>();
        l.eq(BeOrder::getOrderNo,orderNo);
        BeOrder b = beOrderService.getById(orderNo);
        return b!=null?ResponseResult.okResult(b):ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR,"数据失败");
    }
    /**
     * 终止工单->确认终止
     */
    @PostMapping("/qrzz")
    public ResponseResult qrzz(@RequestBody BeOrder beOrder){
        BeHistory history = new BeHistory();
        history.setOrderNo(beOrder.getOrderNo());
        history.setStepID(-1);
        history.setDatee(new Date());
        history.setEmpID(beOrder.getLastEditEmp());
        beOrder.setStepID(-1);
        beHistoryService.save(history);
        return beOrderService.updateById(beOrder)?ResponseResult.okResult("成功!"):ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR,"发送失败!");
    }
    /**
     * 获取用户根据用户编码
     */
    @GetMapping("/getUser")
    public ResponseResult getUserByNo(String userNo){
        LambdaQueryWrapper<BeOrderuser> l1=new LambdaQueryWrapper<>();
        l1.eq(BeOrderuser::getUserNo,userNo);
        BeOrderuser one1 = beOrderuserService.getOne(l1);
        if(one1==null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR,"没有此用户");
        }
        LambdaQueryWrapper<BeOrder> l=new LambdaQueryWrapper<>();
        l.eq(BeOrder::getOrderNo,one1.getOrderNo());
        BeOrder one2 = beOrderService.getOne(l);
        if(one2==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR,"获取失败");
        }
        BeOrder one = beOrderService.getByIdCx(one1.getOrderNo());
        return one!=null?ResponseResult.okResult(one):ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR,"获取失败");
    }

    /**
     * 业扩收费报表数据
     *  list 业扩类型
     *  timeStar 开始日期
     *  timeEnd 结束日期
     * @return 满足条件的数据
     */
    @GetMapping("/getOrderPay")
    public ResponseResult getOrderPay(@RequestParam(defaultValue = "1") int pageNum ,
                                      @RequestParam(defaultValue = "10") int pageSize,
                                      @RequestParam(defaultValue = "")String list,//工单类型
                                      @RequestParam(defaultValue = "")String timeStar,//开始时间
                                      @RequestParam(defaultValue = "")String timeEnd//结束时间
                                       ){
        pageNum=pageNum-1;
        ArrayList<String> lists = new ArrayList<>();
        if(!list.equals("")){
            for (int i = 0; i < list.length(); i++) {
                lists.add(String.valueOf(list.charAt(i)));
            }
        }
        List<orderPay> all = beOrderService.getAll(lists, timeStar, timeEnd,pageNum,pageSize);

        return all.isEmpty()?ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR,"服务器错误"):ResponseResult.okResult(all);
    }

    /**
     * 主页的待处理工单数据
     */
    @GetMapping("/loadOrder")
    public ResponseResult loadOrder(){
        LambdaQueryWrapper<BeOrder> l=new LambdaQueryWrapper<>();
        l.orderByDesc(BeOrder::getLastEditDate);
        l.last("LIMIT 7");
        l.notIn(BeOrder::getStepID,0,-1,8);
        List<BeOrder> list = beOrderService.list(l);
        return list.isEmpty()?ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR,"服务起错误"):ResponseResult.okResult(list);
    }
    /**
     * 业扩工程进度数据
     */
    @GetMapping(value = "SelectAll")
    public ResponseResult SelectAll(HttpServletRequest request) {
        List<OrderTypes> list = beOrderService.getAllOrder();
        return list.isEmpty()?ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR,"服务器错误"):ResponseResult.okResult(list);
    }
}

